package com.xinweisi.dataserver.module.memberdata.memberbehavior;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinweisi.dataserver.common.checkDBName.CheckDBNameService;
import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.dto.MemberBehaviorQueryDTO;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.entity.MemberBehaviorEntity;
import com.xinweisi.dataserver.module.memberdata.memberbehavior.domain.vo.MemberBehaviorVO;
import com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity;
import com.xinweisi.dataserver.third.RedisService;
import com.xinweisi.dataserver.util.DateUtil;
import com.xinweisi.dataserver.util.JsonUtils;
import com.xinweisi.dataserver.util.SmartPageUtil;
import com.xinweisi.dataserver.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MemberBehaviorService {
    @Autowired
    private MemberBehaviorDao memberBehaviorDao;

    private RedisService redisService;
    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Autowired
    private CheckDBNameService checkDBNameService;
    /**
     * 分页获取会员行为
     *
     * @param queryDTO
     * @return
     */
    public ResponseDTO<PageResultDTO<MemberBehaviorVO>> getMemberBehavior(MemberBehaviorQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<Object> entityList = new ArrayList<>();

        for (int i=0;i<queryDTO.getDBNameList().size();i++){
            String DBName = queryDTO.getDBNameList().get(i);
            queryDTO.setDBName(DBName);
            int flag = checkDBNameService.checkDBName(queryDTO.getDBName());
            if(flag==0){
                continue;
            }
            String nowDBName = "daylog_"+ DateUtil.getDate(new Date());

            System.out.println(queryDTO.getDBName());
            System.out.println(queryDTO.getUserId());

            if(nowDBName.equals(DBName)){
                List<MemberBehaviorEntity> list = memberBehaviorDao.selectMemberBehaviorList(queryDTO);
                for (MemberBehaviorEntity obj:list ) {
                    MemberBehaviorVO vo = new MemberBehaviorVO();
                    UserEntity en = UserUtil.userMap.get(obj.getUserId());//获取交易商ByUserId
                    vo.setGameAccount(obj.getUserId());//账号
                    if(en!=null){
                        vo.setGameAccountName(en.getAccount());//账号名称
                    }else {
                        vo.setGameAccountName("");//账号名称
                    }
                    vo.setIpAddress(obj.getIp());//ip
                    vo.setOperTime(obj.getTime());//时间
                    vo.setBehaviorContent(obj.getBehavior());//操作
                    entityList.add(vo);
                }
            }else {
                Map<Object, Object> map = redisService.hmget("Behavior"+DBName+queryDTO.getUserId());
                if(map==null || map.size()==0){
                    List<MemberBehaviorEntity> list1 = (memberBehaviorDao.selectMemberBehaviorList(queryDTO));
                    for (MemberBehaviorEntity obj:list1 ) {
                        MemberBehaviorVO vo = new MemberBehaviorVO();
                        UserEntity en = UserUtil.userMap.get(obj.getUserId());//获取交易商ByUserId
                        vo.setGameAccount(obj.getUserId());//账号
                        if(en!=null){
                            vo.setGameAccountName(en.getAccount());//账号名称
                        }else {
                            vo.setGameAccountName("");//账号名称
                        }
                        vo.setIpAddress(obj.getIp());//ip
                        vo.setOperTime(obj.getTime());//时间
                        vo.setBehaviorContent(obj.getBehavior());//操作
                        entityList.add(vo);
                        String str = JsonUtils.object2json(vo);
                        redisService.hset("Behavior"+DBName+queryDTO.getUserId(),obj.getUserId(),str);
                    }

                }else {
                    for (Object obj:map.keySet()) {
                        String str = map.get(obj.toString()).toString();
                        entityList.add(JsonUtils.parseObject(str,MemberBehaviorVO.class) );
                    }
                }
            }
        }
        page.setTotal(entityList.size());
        PageResultDTO<MemberBehaviorVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, SmartPageUtil.startPage(entityList,queryDTO.getPageNum(),queryDTO.getPageSize()), MemberBehaviorVO.class);
        System.out.println(pageResultDTO.getList().toString());
        return ResponseDTO.succData(pageResultDTO);
    }
}
